package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.itheima.utils.AliyunUtils;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.UUID;

/**
 * 体验套餐管理
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Reference
    private SetmealService setmealService;

    @Autowired//注入操作redis的工具类JedisPool
    private JedisPool jedisPool;

    //存储图片上云端
    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile){

        try {
            //获取上传文件的文件名
            String originalFilename = imgFile.getOriginalFilename();//test.jpg
            //获取文件名最后一个.的位置
            int index = originalFilename.indexOf(".");

            //获取文件的后缀名
            String suffix = originalFilename.substring(index);//.jpg
            //System.out.println(suffix);

            //使用UUID随机生成一个不重复的文件名称，防止同名覆盖
            String fileName = UUID.randomUUID().toString() + suffix;//xxxxx.jpg
            //System.out.println(fileName);

            //利用工具类上传文件
            //七牛云
            //QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);

            //阿里云
            AliyunUtils.uploadAliyun(imgFile.getBytes(),fileName);

            //将随机生成的图片名称存入到redis数据库里面，使用set集合实现
            Jedis jedis = jedisPool.getResource();
            //套餐所有图片名称
            jedis.sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);

            //上传成功，回传文件名，用于数据回显
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,fileName);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
    }


    //新增体检套餐
    @RequestMapping("/add")
    public Result add(@RequestBody Setmeal setmeal,Integer[] checkgroupIds){

        try {
            setmealService.add(setmeal,checkgroupIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
        return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){

        return setmealService.findPage(queryPageBean);
    }
}
